package com.ramos.kuaixiao.ui;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;


public class UserInfoActivity extends BaseActivity implements OnClickListener {
    private final static String TAG = "UserInfoActivity";
    private TextView mDealer;
    private TextView mId;
    private TextView mCompany;
    private TextView mDepartment;
    private TextView mName;
    private TextView mPhone;
    private TextView mEmail;
    private final static int PHOTO_WITH_DATA = 4;
    private final static int PHOTO_WITH_CAMERA = 3;
    LinearLayout title_reback;
    private TextView tv_aboutus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();
        initData();
    }

    private void initView() {
        mDealer = (TextView) findViewById(R.id.dealer);
        mId = (TextView) findViewById(R.id.nickname);
        mName = (TextView) findViewById(R.id.name);
        mPhone = (TextView) findViewById(R.id.phone);
        mEmail = (TextView) findViewById(R.id.email);
        mDepartment = (TextView) findViewById(R.id.department);
        title_reback = (LinearLayout) findViewById(R.id.title_reback);
        title_reback.setOnClickListener(this);
        tv_aboutus = (TextView)findViewById(R.id.title_text);
        tv_aboutus.setText("个人信息");
    }

    private void initData() {
        try {
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("id"))) {
                mId.setText(SharedPreference.getPreferString("id"));
            }
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("dealer"))) {
                mDealer.setText(SharedPreference.getPreferString("dealerName"));
            }
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("name"))) {
                mName.setText(SharedPreference.getPreferString("name"));
            }
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("phone"))) {
                mPhone.setText(SharedPreference.getPreferString("phone"));
            }
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("email"))) {
                mEmail.setText(SharedPreference.getPreferString("email"));
            }
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("org"))) {
                mDepartment.setText(SharedPreference.getPreferString("orgName"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.title_reback:
                finish();
                break;
            default:
                break;
        }
    }

    private void changeIcon() {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(this).setTitle("选择图片");
        DialogInterface.OnClickListener local3 = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                if (paramAnonymousInt == 0) {
                    showImgPick(0);
                    return;
                }
                showImgPick(1);
            }
        };
        localBuilder.setItems(new String[]{"拍照", "从相册中选择"}, local3).show();
    }

    void showImgPick(int paramInt) {
        if (paramInt == 0) {
            if (Environment.getExternalStorageState().equals("mounted")) {
                startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), PHOTO_WITH_CAMERA);
            }
            return;
        }
        Intent localIntent = new Intent("android.intent.action.GET_CONTENT");
        localIntent.setType("image/*");
        startActivityForResult(localIntent, PHOTO_WITH_DATA);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent paramIntent) {
        if (requestCode == PHOTO_WITH_DATA) {
            if (paramIntent == null) {
                return;
            } else {
                Log.d(TAG, "onActivityResult :: requestCode == PHOTO_WITH_DATA");
                ContentResolver resolver = getContentResolver();
                // 照片的原始资源地址
                Uri originalUri = paramIntent.getData();
                try {
                    Bitmap originalbitmap = MediaStore.Images.Media.getBitmap(resolver, originalUri);
                    Bitmap bitmap = zoomBitmap(originalbitmap, findViewById(R.id.act_personal_center_head_img).getWidth(),
                            findViewById(R.id.act_personal_center_head_img).getHeight());
                    // saveBitmap(bitmap);
                    // 写一个方法将此文件保存到本应用下面啦
                    savePicture(bitmap);
                    if (bitmap != null) {
                        ((ImageView) findViewById(R.id.act_personal_center_head_img)).setImageBitmap(bitmap);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (requestCode == PHOTO_WITH_CAMERA) {
            if (paramIntent != null) {
                Log.d(TAG, "onActivityResult :: requestCode == PHOTO_WITH_CAMERA");
                Bundle extras = paramIntent.getExtras();
                if (extras != null) {
                    // 这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
                    Bitmap originalbitmap = extras.getParcelable("data");
                    if (originalbitmap != null) {
                        Bitmap bitmap = zoomBitmap(originalbitmap, findViewById(R.id.act_personal_center_head_img).getWidth(),
                                findViewById(R.id.act_personal_center_head_img).getHeight());
                        Log.d(TAG, "PHOTO_WITH_CAMERA");
                        // 写一个方法将此文件保存到本应用下面啦
                        savePicture(bitmap);
                        if (bitmap != null) {
                            ImageView iv0 = (ImageView) findViewById(R.id.act_personal_center_head_img);
                            iv0.setImageBitmap(bitmap);

                        }
                    }
                }
            }
        }
    }

    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    /**
     * 保存图片到本应用下
     **/
    @SuppressLint("SdCardPath")
    private void savePicture(Bitmap bitmap) {
        File icon = new File("/mnt/sdcard/icon.png");
        try {
            BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream(icon));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, localBufferedOutputStream);
            localBufferedOutputStream.flush();
            localBufferedOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
